package com.typhoon.spring_shiro.dto.response;

import java.io.Serializable;

import com.typhoon.spring_shiro.constants.EntityError;

public class ResponseBase implements Serializable {
	private static final long serialVersionUID = 7825036327382479394L;
	public static final String SUCCESS = "4001";
	public static final String ERROR_CODE = "4000";
	private String code = "4001";
	private String reason;
	private String result;
	private Object attach;

	public ResponseBase() {
	}

	public ResponseBase(String code, String reason) {
		this.code = code;
		this.reason = reason;
	}

	public String getCode() {
		return this.code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getReason() {
		return this.reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public <T extends ResponseBase> T error(String reason) {
		this.code = ERROR_CODE;
		this.reason = reason;
		return (T) this;
	}

	public <T extends ResponseBase> T error(String code, String reason) {
		this.code = code;
		this.reason = reason;
		return (T) this;
	}

	@SuppressWarnings("unchecked")
	public <T extends ResponseBase> T error(EntityError error) {
		if (null == error) {
			this.code = ERROR_CODE;
			return (T) this;
		}
		this.code = String.valueOf(error.getCode());
		this.reason = error.getMsg();
		return (T) this;
	}

	public void success(String reason) {
		this.code = SUCCESS;
		this.reason = reason;
	}

	public String getResult() {
		return this.result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public boolean isSuccess() {
		return SUCCESS.equals(this.code);
	}

	public String toString() {
		return "code:" + this.code + ",result:" + this.result + ",reason:" + this.reason;
	}

	public Object getAttach() {
		return this.attach;
	}

	public void setAttach(Object attach) {
		this.attach = attach;
	}
}
